package sk.tuke.homescreen;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ing. Tibor Fecko on 14.4.2014.
 */
public class AppAdapter { 

    private Context mContext = null;
    private AppInfo[] mContainer = null;
    private List<ApplicationInfo> mApplications = null;
    private final int ICONS_PER_SCREEN = 6;
    private int index = 0;

    public AppAdapter(Context mContext_in){
        this.mContext = mContext_in;
        mContainer = new AppInfo[ICONS_PER_SCREEN];
        mApplications = new ArrayList<ApplicationInfo>();
        
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> pkgAppsList = mContext.getPackageManager().queryIntentActivities( mainIntent, 0);
        //mContext.getPackageManager().getInstalledApplications(PackageManager.GET_GIDS);
        for(ResolveInfo i : pkgAppsList){
        	if (mContext.getPackageManager().getLaunchIntentForPackage(i.activityInfo.packageName) != null) {
                mApplications.add(i.activityInfo.applicationInfo);
            }
        }
    }

    public AppInfo[] getCurrentSet(){
        for(int i = 0; i<ICONS_PER_SCREEN; i++){
            //ToDo: DOkoncit prepinanie a ataktiez rozne moznosti ktore mozu tuna vzniknut
        	if(i+index >= mApplications.size()){
        		//index = ICONS_PER_SCREEN - i;
        		index = -i;
        	}
            mContainer[i] = new AppInfo(mContext, mApplications.get(i + index).loadIcon(mContext.getPackageManager()), mApplications.get(i+index).loadLabel(mContext.getPackageManager()).toString(), mApplications.get(i + index).packageName);
        }
        if(index < 0) index = 0;
        return mContainer;
    }
    
 
    public void ScrollDown(){
    	index+=ICONS_PER_SCREEN;
    }


}